//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EndEulerDiscretization.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/StochasticProcess.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (boost::shared_ptr<QuantLib::EndEulerDiscretization>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndEulerDiscretization = NULL;
#endif
	_ppEndEulerDiscretization = &childNative;
    
}
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (QuantLib::EndEulerDiscretization& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phEndEulerDiscretization = NULL;
#endif
	_ppEndEulerDiscretization = new boost::shared_ptr<QuantLib::EndEulerDiscretization> (&childNative);
    
    _EndEulerDiscretizationOwner = owner;
    
}

Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (CEndEulerDiscretization^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndEulerDiscretization = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEndEulerDiscretization = new boost::shared_ptr<QuantLib::EndEulerDiscretization> (copy->GetShared());
        
    }
}
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndEulerDiscretization = NULL;
#endif
	if (!t->IsSubclassOf(CEndEulerDiscretization::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (QuantLib::Handle<QuantLib::EndEulerDiscretization>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEndEulerDiscretization = &childNative;
	_ppEndEulerDiscretization = &static_cast<boost::shared_ptr<QuantLib::EndEulerDiscretization>>(childNative.currentLink());
    
    _EndEulerDiscretizationOwner = owner;
}
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (QuantLib::Handle<QuantLib::EndEulerDiscretization> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEndEulerDiscretization = &childNative;
	_ppEndEulerDiscretization = &static_cast<boost::shared_ptr<QuantLib::EndEulerDiscretization>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CEndEulerDiscretization::CEndEulerDiscretization (QuantLib::EndEulerDiscretization childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndEulerDiscretization = NULL;
#endif
	_ppEndEulerDiscretization = new boost::shared_ptr<QuantLib::EndEulerDiscretization> (new QuantLib::EndEulerDiscretization (childNative));
    
}
#endif

Cephei::QL::Processes::CEndEulerDiscretization::~CEndEulerDiscretization ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppEndEulerDiscretization != NULL)
    {
	    delete _ppEndEulerDiscretization;
        _ppEndEulerDiscretization = NULL;
    }
}
Cephei::QL::Processes::CEndEulerDiscretization::!CEndEulerDiscretization ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppEndEulerDiscretization != NULL)
    {
	    delete _ppEndEulerDiscretization;
    }
}
QuantLib::EndEulerDiscretization& Cephei::QL::Processes::CEndEulerDiscretization::GetReference ()
{
    if (_ppEndEulerDiscretization == NULL) throw REFNEW NativeNullException ();
	return **_ppEndEulerDiscretization;
}
boost::shared_ptr<QuantLib::EndEulerDiscretization>& Cephei::QL::Processes::CEndEulerDiscretization::GetShared ()
{
    if (_ppEndEulerDiscretization == NULL) throw REFNEW NativeNullException ();
	return *_ppEndEulerDiscretization;
}
QuantLib::EndEulerDiscretization* Cephei::QL::Processes::CEndEulerDiscretization::GetPointer ()
{
    if (_ppEndEulerDiscretization == NULL) throw REFNEW NativeNullException ();
	return &**_ppEndEulerDiscretization;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EndEulerDiscretization>& Cephei::QL::Processes::CEndEulerDiscretization::GetHandle ()
{
	if (_phEndEulerDiscretization == NULL)
	{
		_phEndEulerDiscretization = new Handle<QuantLib::EndEulerDiscretization> (*_ppEndEulerDiscretization);
	}
	return *_phEndEulerDiscretization;
}
#endif
bool Cephei::QL::Processes::CEndEulerDiscretization::HasNative () 
{
	return (_ppEndEulerDiscretization != NULL);
}

Double Cephei::QL::Processes::CEndEulerDiscretization::Diffusion (Cephei::QL::IStochasticProcess1D^ process, Double t0, Double x0, Double dt)
{
    CStochasticProcess1D^ _Cprocess;
    try
    {
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        QuantLib::StochasticProcess1D& _process = static_cast<QuantLib::StochasticProcess1D&> (_Cprocess->GetReference ()); 
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndEulerDiscretization)->diffusion ( _process,  _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Double Cephei::QL::Processes::CEndEulerDiscretization::Drift (Cephei::QL::IStochasticProcess1D^ process, Double t0, Double x0, Double dt)
{
    CStochasticProcess1D^ _Cprocess;
    try
    {
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        QuantLib::StochasticProcess1D& _process = static_cast<QuantLib::StochasticProcess1D&> (_Cprocess->GetReference ()); 
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndEulerDiscretization)->drift ( _process,  _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Double Cephei::QL::Processes::CEndEulerDiscretization::Variance (Cephei::QL::IStochasticProcess1D^ process, Double t0, Double x0, Double dt)
{
    CStochasticProcess1D^ _Cprocess;
    try
    {
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        QuantLib::StochasticProcess1D& _process = static_cast<QuantLib::StochasticProcess1D&> (_Cprocess->GetReference ()); 
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndEulerDiscretization)->variance ( _process,  _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

